---
--- 插件入口handler
--- Created by E.T.
--- DateTime: 2018/10/25 11:35
---
local BasePlugin = require "kong.plugins.base_plugin"
local tag = require "kong.plugins.gray.rule.tag"
local ip = require "kong.plugins.gray.rule.ip"
local users = require "kong.plugins.gray.rule.users"
local eureka = require "kong.plugins.gray.tools.eureka_apps"
local GrayHandler = BasePlugin:extend()
local ngx_log = ngx.log
local DEBUG = ngx.NOTICE
GrayHandler.PRIORITY = 1001
GrayHandler.VERSION = "1.0.1"

function GrayHandler:new()
    GrayHandler.super.new(self, "gray")
end

function GrayHandler:init_worker()
    --eureka.get_eureka_apps()
    --ngx_log(DEBUG, "executing plugin \"", self._name, "\": init_worker")
end

function GrayHandler:rewrite(conf)
    --ngx_log(DEBUG, "executing plugin \"", self._name, "\": rewrite")
end

function GrayHandler:access(conf)
    GrayHandler.super.access(self)
    if conf.rule == "tag-gray" then
        tag.execute(conf)
    end
    if conf.rule == "ip" then
        ip.execute(conf)
    end
    if conf.rule == "users" then
        users.execute(conf)
    end
    --ngx_log(DEBUG, "access \"", self._name, "\": access")
end

--function GrayHandler:header_filter()
    --ngx_log(DEBUG, "上游状态",kong.service.response.get_status())
    --ngx_log(DEBUG, "下游状态",kong.response.get_status())
    --ngx_log(DEBUG, "来源",kong.response.get_source())
    --ngx_log(DEBUG, "executing plugin \"", self._name, "\": header_filter")
--end
--function GrayHandler:body_filter()
--  ngx_log(DEBUG, "executing plugin \"", self._name, "\": body_filter")
--end

--function GrayHandler:log()
    --ngx_log(DEBUG, "executing plugin \"", self._name, "\": log")
--end

return GrayHandler